package com.qwc.blog.web.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.common.constant.ConfigConstant;
import com.qwc.blog.common.constant.ResponseConstant;
import com.qwc.blog.common.exception.ResponseException;
import com.qwc.blog.entity.SysConfig;
import com.qwc.blog.entity.dto.PwdUpdateDto;
import com.qwc.blog.entity.dto.SysConfigDto;
import com.qwc.blog.entity.dto.SysConfigRequestDto;
import com.qwc.blog.service.ILoginService;
import com.qwc.blog.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 * 配置表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2022-01-24
 */
@AdminController("/sysConfig")
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ILoginService loginService;

    @GetMapping("/page")
    public ResponseEntity<Object> page(SysConfigRequestDto<SysConfig> dto){
        SysConfigRequestDto<SysConfig> page = sysConfigService.page(dto, Wrappers.<SysConfig>lambdaQuery()
                .eq(CharSequenceUtil.isNotBlank(dto.getStatus()), SysConfig::getStatus, dto.getStatus())
                .like(CharSequenceUtil.isNotBlank(dto.getName()), SysConfig::getName, dto.getName())
                .like(CharSequenceUtil.isNotBlank(dto.getConfigKey()), SysConfig::getConfigKey, dto.getConfigKey())
                .orderByDesc(SysConfig::getUpdateTime)
        );
        for(Iterator<SysConfig> iterator = page.getRecords().iterator();iterator.hasNext();){
            if (ConfigConstant.LOGIN.equals(iterator.next().getName())) {
                iterator.remove();
            }
        }
        return ResponseEntity.ok(page);
    }

    @PutMapping
    public ResponseEntity<Object> update(@Validated @RequestBody SysConfigDto dto){
        if (Objects.isNull(dto.getId())) {
            throw new ResponseException(ResponseConstant.ID_IS_NULL);
        }
        SysConfig config = new SysConfig();
        BeanUtil.copyProperties(dto,config);
        return ResponseEntity.ok(sysConfigService.updateById(config));
    }

    @PostMapping
    public ResponseEntity<Object> add(@Validated @RequestBody SysConfigDto dto){
        SysConfig config = new SysConfig();
        BeanUtil.copyProperties(dto,config);
        return ResponseEntity.ok(sysConfigService.save(config));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Object> getById(@PathVariable @NotNull(message = "Id不能为空") Integer id){
        SysConfig byId = sysConfigService.getById(id);
        SysConfigDto sysConfigDto = new SysConfigDto();
        BeanUtil.copyProperties(byId,sysConfigDto);
        return ResponseEntity.ok(sysConfigDto);
    }

    @DeleteMapping("{ids}")
    public ResponseEntity<Object> deleteByIds(@PathVariable @NotNull(message = "请选择删除数据") Integer[] ids){
        return ResponseEntity.ok(sysConfigService.removeByIds(Arrays.asList(ids)));
    }

    @PutMapping("/pwd")
    public ResponseEntity<Object> updatePassword(@Validated @RequestBody PwdUpdateDto dto){
        return ResponseEntity.ok(loginService.updatePassword(dto));
    }
}
